package com.qf.fmall.product.controller;

import com.qf.fmall.product.entity.ProductParams;
import com.qf.fmall.product.service.IProductCommentsService;
import com.qf.fmall.product.service.IProductParamsService;
import com.qf.fmall.product.service.IProductService;
import com.qf.fmall.product.vo.ProductCommontVo;
import com.qf.fmall.product.vo.ProductDetailVo;
import com.qf.fmall.product.vo.ProductScountVo;
import com.qf.fmall.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wbw
 * @since 2023-11-29
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductParamsService productParamsService;
    @Autowired
    private IProductCommentsService productCommentsService;
    @GetMapping("/detail-commontscount/{id}")
    public ResultVo detailcommontscount(@PathVariable("id") Integer id){
        log.info("商品好评率接口，id={}",id);
        if (id==null){
            return ResultVo.notok(-10005, "商品id为空");
        }
        ProductScountVo data = productCommentsService.detailcommontscount(id);
        return ResultVo.ok(10000,"success",data);
    }
    @GetMapping("/detail-commonts/{id}")
    public ResultVo detailcommonts(@PathVariable("id") Integer id, Integer pageNum, Integer limit){
        log.info("查询商品评价,id={},pageNum={},limit={}",id,pageNum,limit);

        if (id==null){
            return ResultVo.notok(-10005, "商品id为空");
        }

        if (pageNum<=0 || limit<=0){
            return ResultVo.notok(-10006, "分页的页号或者页大小不能小于0");
        }
        ProductCommontVo data = productCommentsService.detailcommonts(id,pageNum,limit);


        return ResultVo.ok(10000,"success",data);
    }
    @GetMapping("/detail-params/{id}")
    public ResultVo detailparams(@PathVariable("id") Integer id){
        log.info("商品参数查询,id={}",id);
        if (id==null){
            return ResultVo.notok(-10005, "商品id为空");
        }
        ProductParams data = productParamsService.detailparams(id);
        return ResultVo.ok(10000, "success", data);
    }
    @GetMapping("/detail-info/{id}")
    public ResultVo detailInfo(@PathVariable("id") Integer id){
        //1. 打印日志，把该接收到的参数打印出来
        log.info("查询商品详细信息，id={}",id);
        //2. 进行必要的参数校验
        if (id==null){
            return ResultVo.notok(-10005, "商品id为空");
        }
        //3. 调用 service层的对象进行业务处理
        ProductDetailVo data = productService.detailInfo(id);
        //4. 返回 json
        return ResultVo.ok(10000, "success", data);
    }
}
